package com.smarthospital.dao.daoService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarthospital.dao.mapper.InquiryOrderDao;
import com.smarthospital.entity.pojo.InquiryOrder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InquiryOrderDaoService{

    @Resource
    private InquiryOrderDao inquiryOrderDao;

    public int insert(InquiryOrder pojo){
        return inquiryOrderDao.insert(pojo);
    }

    public int insertSelective(InquiryOrder pojo){
        return inquiryOrderDao.insertSelective(pojo);
    }

    public int insertList(List<InquiryOrder> pojos){
        return inquiryOrderDao.insertList(pojos);
    }

    public int update(InquiryOrder pojo){
        return inquiryOrderDao.update(pojo);
    }

    public PageInfo<InquiryOrder> querMyInquiryOrder(Long inquiryManId, Integer doctorLevel, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        InquiryOrder buider = InquiryOrder.builder().inquiryManId(inquiryManId).doctorLevel(doctorLevel).build();
        List<InquiryOrder> list = inquiryOrderDao.queryOrder(buider);
        return PageInfo.of(list);
    }

    public List<InquiryOrder> queryOrder(InquiryOrder order) {
        return inquiryOrderDao.queryOrder(order);
    }

    public InquiryOrder getOrderById(Long id) {
        return inquiryOrderDao.selectById(id);
    }

    //用in查询 查出这些医生关联的订单
    public List<InquiryOrder> queryOrderWithDoIds(List<Long> doIds) {
        return inquiryOrderDao.queryOrderWithDoIds(doIds);
    }

    /**
     * 判断是否有白嫖资格 分医生的
     * 白嫖资格  最近2次的订单都没有用满20分钟
     * @param orderList
     * @return
     */
    public boolean hasZeroFeeMethod(List<InquiryOrder> orderList) {
        int limit = 3;
        //根据订单创建时间倒序排序
        orderList.sort(Comparator.comparing(InquiryOrder::getCreateTime).reversed());
        List<InquiryOrder> collect = orderList.stream().filter(d -> d.getStatus() == 1).collect(Collectors.toList());
        Iterator<InquiryOrder> iterator = collect.iterator();
        while (iterator.hasNext()){
            limit--;
            InquiryOrder next = iterator.next();
            if (next.getLeftVedioTime()>0&&next.getTotal()==0) {
                continue;
            }
            if (next.getLeftVedioTime()>0&&next.getTotal()>0) {
                return true;
            }

            if (limit == 0) {
                break;
            }
        }
        return false;
    }

    public void closeOrder(String orderId) {
        inquiryOrderDao.closeOrder(orderId);
    }

    public List<InquiryOrder> queryByMajor(String major, Integer status) {
        return inquiryOrderDao.queryByMajor(major,status);
    }

    public InquiryOrder queryById(Long orderId) {
        return inquiryOrderDao.queryById(orderId);
    }
}
